<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/29
 * Time: 14:58
 */
require 'vendor/autoload.php';
require 'connect.php';
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Slim\PDO\Database;
use Slim\PDO\Statement;
use Slim\PDO\Statement\SelectStatement;
$app = new \Slim\App();


$app->get('/getOrders1',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $status=$request->getParam('status');
    $database=localhost();
        $selectStatement = $database->select()
            ->from('orders')
            ->where('status','=',$status);
        $stmt = $selectStatement->execute();
        $data1= $stmt->fetchAll();
        if($data1!=null){
        for($x=0;$x<count($data1);$x++){
            $selectStatement = $database->select()
                ->from('contact')
                ->where('id','=',$data1[$x]['sender_id']);
            $stmt = $selectStatement->execute();
            $data2= $stmt->fetch();
            $data1[$x]['sender']=$data2;
            $selectStatement = $database->select()
                ->from('contact')
                ->where('id','=',$data1[$x]['receiver_id']);
            $stmt = $selectStatement->execute();
            $data3= $stmt->fetch();
            $data1[$x]['receiver']=$data3;
            $selectStatement = $database->select()
                ->from('place')
                ->where('id','=',$data1[$x]['send_place_id']);
            $stmt = $selectStatement->execute();
            $data4= $stmt->fetch();
            $data1[$x]['send_place']=$data4;
            $selectStatement = $database->select()
                ->from('place')
                ->where('id','=',$data1[$x]['receive_place_id']);
            $stmt = $selectStatement->execute();
            $data5= $stmt->fetch();
            $data1[$x]['receive_place']=$data5;
        }
        }
        return $response->withJson(array("result" => "0", "desc" => "success",'orders'=>$data1));
});

$app->get('/getOrder',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $id=$request->getParam('id');
    $database=localhost();
    $selectStatement = $database->select()
        ->from('orders')
        ->where('id','=',$id);
    $stmt = $selectStatement->execute();
    $data1= $stmt->fetch();
    if($data1!=null){
            $selectStatement = $database->select()
                ->from('contact')
                ->where('id','=',$data1['sender_id']);
            $stmt = $selectStatement->execute();
            $data2= $stmt->fetch();
            $data1['sender']=$data2;
            $selectStatement = $database->select()
                ->from('contact')
                ->where('id','=',$data1['receiver_id']);
            $stmt = $selectStatement->execute();
            $data3= $stmt->fetch();
            $data1['receiver']=$data3;
            $selectStatement = $database->select()
                ->from('place')
                ->where('id','=',$data1['send_place_id']);
            $stmt = $selectStatement->execute();
            $data4= $stmt->fetch();
            $data1['send_place']=$data4;
            $selectStatement = $database->select()
                ->from('place')
                ->where('id','=',$data1['receive_place_id']);
            $stmt = $selectStatement->execute();
            $data5= $stmt->fetch();
            $data1['receive_place']=$data5;
        }
    return $response->withJson(array("result" => "0", "desc" => "success",'order'=>$data1));
});



$app->options('/alterOrder1',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $response=$response->withAddedHeader("Access-Control-Allow-Methods", "PUT");
    return $response;
});


$app->put('/alterOrder1',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $body = $request->getBody();
    $body=json_decode($body);
    $id=$body->id;
    $status=$body->status;
    if($id!=null||$id!=""){
        $updateStatement = $database->update(array("status"=>$status))
            ->table('orders')
            ->where('id','=',$id);
        $affectedRows = $updateStatement->execute();
        return $response->withJson(array("result" => "0", "desc" => "success"));
    }else{
        return $response->withJson(array("result"=>"1","desc"=>"id为空"));
    }
});



$app->run();

function localhost()
{
    return connect();
}


?>